/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.agreement.web.serviceinvoice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ch.qos.logback.core.util.StringCollectionUtil;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.agreement.entity.serviceinvoice.TserviceInvoiceDetail;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.service.customer.CustomerService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.sys.service.UserService;
import io.netty.util.internal.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.agreement.entity.serviceinvoice.TserviceInvoice;
import com.jeesite.modules.agreement.service.serviceinvoice.TserviceInvoiceService;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务发票Controller
 * @author shengbin
 * @version 2021-03-04
 */
@Controller
@RequestMapping(value = "${adminPath}/agreement/serviceinvoice/tserviceInvoice")
public class TserviceInvoiceController extends BaseController {

	@Autowired
	private TserviceInvoiceService tserviceInvoiceService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private EmpUserService empUserService;
	@Autowired
	private ServiceOrderService serviceOrderService;
	@Autowired
	private UserService userService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TserviceInvoice get(String serviceInvoiceId, boolean isNewRecord) {
		return tserviceInvoiceService.get(serviceInvoiceId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("agreement:serviceinvoice:tserviceInvoice:view")
	@RequestMapping(value = {"list", ""})
	public String list(TserviceInvoice tserviceInvoice, Model model) {
		List<Customer> listCustomer= customerService.findList(new Customer());
		model.addAttribute("listUser", empUserService.findList(new EmpUser()));
		model.addAttribute("listCustomer", listCustomer);
		model.addAttribute("tserviceInvoice", tserviceInvoice);
		return "modules/agreement/serviceinvoice/tserviceInvoiceList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("agreement:serviceinvoice:tserviceInvoice:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TserviceInvoice> listData(TserviceInvoice tserviceInvoice, HttpServletRequest request, HttpServletResponse response) {
		TserviceInvoiceDetail tserviceInvoiceDetail = tserviceInvoice.getTserviceInvoiceDetail();

		if (StringUtils.isNotBlank(tserviceInvoiceDetail.getInvoiceItem())) {
		}

		tserviceInvoice.setPage(new Page<>(request, response));
		Page<TserviceInvoice> page = tserviceInvoiceService.findPage(tserviceInvoice);
		return page; //tserviceInvoiceService.findListByQuery(tserviceInvoice);
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("agreement:serviceinvoice:tserviceInvoice:view")
	@RequestMapping(value = "form")
	public String form(TserviceInvoice tserviceInvoice, Model model) {
		String serviceOrderId = tserviceInvoice.getServiceOrderId();

		List<ServiceOrderDetail> listFrameNumber = new ArrayList<>();
		if (StringUtils.isNotBlank(serviceOrderId)) {
			ServiceOrder serviceOrder=new ServiceOrder();
			serviceOrder.setServiceOrderCode(serviceOrderId);
			List<ServiceOrder> findList = serviceOrderService.findList(serviceOrder);
			serviceOrder=findList.get(0);
			serviceOrder=serviceOrderService.get(serviceOrder);
			listFrameNumber = serviceOrder.getServiceOrderDetailList();
		}

		List<Customer> listCustomer = customerService.findList(new Customer());
		model.addAttribute("listUser", userService.findList(new User()));
		model.addAttribute("listCustomer", listCustomer);
		model.addAttribute("tserviceInvoice", tserviceInvoice);
		model.addAttribute("listFrameNumber", listFrameNumber);
		return "modules/agreement/serviceinvoice/tserviceInvoiceForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("agreement:serviceinvoice:tserviceInvoice:view")
	@RequestMapping(value = "detail")
	public String detail(TserviceInvoice tserviceInvoice, Model model) {
		List<Customer> listCustomer= customerService.findList(new Customer());

		model.addAttribute("listUser", userService.findList(new User()));
		model.addAttribute("listCustomer", listCustomer);
		model.addAttribute("tserviceInvoice", tserviceInvoice);
		return "modules/agreement/serviceinvoice/tserviceInvoiceDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("agreement:serviceinvoice:tserviceInvoice:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TserviceInvoice tserviceInvoice) {
		tserviceInvoiceService.save(tserviceInvoice);
		return renderResult(Global.TRUE, text("保存服务发票成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("agreement:serviceinvoice:tserviceInvoice:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TserviceInvoice tserviceInvoice) {
		tserviceInvoice.setStatus(TserviceInvoice.STATUS_DISABLE);
		tserviceInvoiceService.updateStatus(tserviceInvoice);
		return renderResult(Global.TRUE, text("停用服务发票成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("agreement:serviceinvoice:tserviceInvoice:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TserviceInvoice tserviceInvoice) {
		tserviceInvoice.setStatus(TserviceInvoice.STATUS_NORMAL);
		tserviceInvoiceService.updateStatus(tserviceInvoice);
		return renderResult(Global.TRUE, text("启用服务发票成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("agreement:serviceinvoice:tserviceInvoice:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TserviceInvoice tserviceInvoice) {
		tserviceInvoiceService.delete(tserviceInvoice);
		return renderResult(Global.TRUE, text("删除服务发票成功！"));
	}
	
}